<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

/**
 * Class WechatMenu
 * @package App\Models
 */
class WechatMenu extends Model
{
    public static $mainMenuRules = [
        'name' => 'required'
    ];

    public static $clickMenuRules = [
        'name' => 'required',
        'key' => 'required',
        'text' => 'required'
    ];

    public static $viewMenuRules = [
        'name' => 'required',
        'url' => 'required|url'
    ];

    public static $mediaMenuRules = [
        'name' => 'required',
        'media_id' => 'required'
    ];

    public static $miniprogramMenuRules = [
        'name' => 'required',
        'url' => 'required|url',
        'pagepath' => 'required',
        'miniprogram_appid' => 'required'
    ];

    protected $fillable = [
        'name',
        'type',
        'key',
        'text',
        'url',
        'sort',
        'media_id',
        'pagepath',
        'miniprogram_appid',
        'sub_id',
        'group_id',
        'is_enable'
    ];

    public function scopeIsEnable($query)
    {
        return $query->where('is_enable', '=', 1);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function children()
    {
        return $this->hasMany(self::class,'sub_id');
    }

    /**
     * @param $groupId
     * @return array
     * 返回当前活动下已启用的菜单id列表
     */
    public static function activeCount($groupId)
    {
        return self::where('sub_id', '=', 0)
                    ->where('group_id', '=', $groupId)
                    ->isEnable()->get(['id'])
                    ->map(function ($val) {
                        return $val->id;
                    })->toArray();
    }
}
